/**
 * Created by zyy on 15/5/26.
 * zhangyuyu@superjia.com
 */
(function ($) {
    'use strict'

    window.log = function (message) {
        if (console && console.log) {
            console.log(message);
        }
    };

    var util = {
        templatePackage: function () {
            template.renderHtml = function (source, options) {
                var render = template.compile(source);
                return render(options);
            }
        }(),

        isEmail: function (v) {
            return /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/.test(v);
        },

        isMobile: function (v) {
            return /^1[3|4|5|7|8]\d{9}$/.test(v);
        },

        getParaValue: function (param, str) {
            var hashs = (str ? str : location.hash.substr(2)).split('&')
            var findValue
            _.each(hashs, function (hash) {
                var h = hash.split('=')
                if (h[0] === param) {
                    findValue = h[1]
                    return false
                }
            })
            return findValue
        },

        locationReload: function (url) {
            var href = window.location.href;

            if (href == url) {
                window.location.reload();
            } else {
                window.location.href = url;
            }
        },

        __localStoreKey: 'iwjw_storage',

        getLocalStore: function (key) {
            var self = this
            var s = store.get(self.__localStoreKey) || {}
            var o = {}
            if (_.isObject(key)) {
                for (var p in key) {
                    o[p] = s[p] || key[p]
                    /*default value*/
                }
            } else {
                o = s[key]
            }
            return o
        },

        setLocalStore: function (key, value) {
            var self = this
            var s = store.get(self.__localStoreKey) || {}
            if (_.isObject(key)) {
                for (var p in key) {
                    s[p] = key[p]
                }
            } else {
                s[key] = value
            }
            store.set(self.__localStoreKey, s)
            return self
        }

    }

    // export public method

    App.common.modules.util = util

})(jQuery)